************************************************************************
************************************************************************
***** Replication dofile for dyadic trade data			****************
***** User needs to replace directories with own paths	****************
************************************************************************
************************************************************************

**#**************************************************
** Cleaning of IMF imports data ************************************
************************************************************************

** Define Path
cd "C:\\..."

** Macro path for where raw import data (in csv format) is stored
	global all_files "C:\\..."
	
** (1) Read in all csv files and append

* read in every .dta file in specified directory
	local files : dir "$all_files" files "*.csv"

	local u 1
	qui foreach file in `files' {
		import delimited using "$all_files\`file'", clear
		rename time_period year

		tempfile _`u'
		save `_`u''
		noisily di "Processing `file' as _`u'"	
		local u = `u'+1
	}

	use `_1', clear
	local k = `u'-1
	forvalues i = 2/`k'{
		di `i'
		append using `_`i''
	}

*** create trade pair string and traid pair id (numeric)
	split trade_id, p("_")
	rename trade_id1 country_a 
	rename trade_id2 country_b
	gen trade_pair = country_a + "_" + country_b
	drop trade_id 
	sort trade_pair
	egen trade_id = group(trade_pair)


	gen indicator = "TMG_CIF_USD"
	drop v1


	xtset trade_id year
	xtdescribe


** merge with dataset containing one observation for every year from 1948 - 2021 (here called "temp.dta") 	// takes much time!
// why is this necessary? To ensure that there are the merge command doesn't exclude any pair for which there are either only exports or either only imports reported

	sum trade_id
	local o = r(max)

	qui forval i = 1/`o'{
		preserve
			keep if trade_id == `i'
			tempfile current
			save `current'
			use "C:\\...\\temp.dta", clear
			merge 1:1 year using `current', nogen
			gen negyear = -year
			sort negyear
			*carryforward trade_id, replace
			carryforward trade_pair, replace
			carryforward country_a, replace
			carryforward country_b, replace
			sort year
			carryforward trade_id, replace 
			carryforward trade_pair, replace
			drop negyear
			tempfile _`i'
			save `_`i''
			noi di "`i' of `o'"
		restore
	}

	use `_1', clear

	forval i = 2/`o'{
		append using `_`i''
	}

	drop indicator
	gen indicator = "TMG_CIF_USD"

** rename variables
	rename obs_value value_imports
	rename obs_status import_status
	rename indicator indicator_imports
	
** save
	save imports.dta, replace
	

**#**************************************************
** Cleaning of IMF exports data ************************************
************************************************************************


** Define Path
cd "C:\\..."

global all_files "C:\\..."

** (1) Read in all csv files and append

* read in every .dta file in specified directory
	local files : dir "$all_files" files "*.csv"

	local u 1
	qui foreach file in `files' {
		import delimited using "$all_files\`file'", clear
		* rename variable year
		rename time_period year
		tempfile _`u'
		save `_`u''
		local u = `u'+1
	}

** read in local macros and append
	use `_1', clear
	local k = `u'-1
	forvalues i = 2/`k'{
		di `i'
		append using `_`i''
	}

*** create trade pair string and traid pair id (numeric); NEEDS TO BE OTHER WAY AROUND THAN IMPORTS
	split trade_id, p("_")
	rename trade_id1 country_b 
	rename trade_id2 country_a
	gen trade_pair = country_a + "_" + country_b
	drop trade_id country_b country_a
	sort trade_pair
	egen trade_id = group(trade_pair)


** Indicate form of export data
	gen indicator = "TXG_FOB_USD"

** drop unnecessary variables
	drop v1

** Make sure that trade_id and year uniquely identify observations
	xtset trade_id year
	xtdescribe

** create string indicator
	drop indicator
	gen indicator = "TXG_FOB_USD"

** rename variables
	rename obs_value value_exports
	rename obs_status exports_status
	rename indicator indicator_exports
	
** save export side
	save exports.dta, replace


*************** Merge **************************************+
cd  "C:\\..."

use "C:\\...\\imports.dta", clear

** merge import data with export data
	merge 1:1 trade_pair year using "C:\\...\\exports.dta"

** drop unnecessary variables
	cap drop trade_id country_a country_b

** Name individual trading countries, allocate abbreviation and full name
** Country abbreviation code is ISO Alpha-2 codes
** one source: https://www.nationsonline.org/oneworld/country_code_list.htm

	split trade_pair, p("_")
	rename trade_pair1 country_a
	rename trade_pair2 country_b


preserve
	import excel "C:\\...", clear first
	*drop v1
	rename country_abbr country_a
	rename country_name countryA_name_uni
	tempfile abbr_a
	save `abbr_a'
restore
merge m:1 country_a using `abbr_a', keepusing(countryA_name_uni) nogen


preserve
	import excel "C:\\...", clear first
	rename country_abbr country_b
	rename country_name countryB_name_uni
	tempfile abbr_b
	save `abbr_b'
restore
merge m:1 country_b using `abbr_b', keepusing(countryB_name_uni) nogen


** give trade id (numeric)
	sort trade_pair
	egen trade_id = group(trade_pair)

** drop if missing trade_pair
	drop if missing(trade_id)

** check panel so that unique identifiers exists for every observation 
	xtset trade_id year
	
*xtdescribe

** rename
	rename value_exports flowBAB
	rename value_imports flowBAA
	rename import_status pr_flowBAA
	rename exports_status pr_flowBAB
	rename indicator_imports flowBAA_indicator
	rename indicator_exports flowBAB_indicator
	
** labeling & cleaning
	label var flowBAA "Reported imports of country A from country B"
	label var flowBAB "Reported exports of country B to country A"

	gen pr_flowBAA2 = 1 if pr_flowBAA == "e"
	replace pr_flowBAA2 = 0 if missing(pr_flowBAA) & !missing(flowBAA)

	drop pr_flowBAA 
	rename pr_flowBAA2 pr_flowBAA

	gen pr_flowBAB2 = 1 if pr_flowBAB == "e"
	replace pr_flowBAB2 = 0 if missing(pr_flowBAB) & !missing(flowBAB)
	drop pr_flowBAB
	rename pr_flowBAB2 pr_flowBAB


	label define status 0 "Information directly sourced from country data" 1 "Data estimated using counterpart information only" 
	label values pr_flowBAB status
	label values pr_flowBAA status

	order trade_id trade_pair countryA_name_uni countryB_name_uni year flowBAA flowBAA_indicator pr_flowBAA flowBAB flowBAB_indicator pr_flowBAB 

	rename _merge merge_import_export

** save dyadic data set 
save "C:\\...\\mirrortrade_dyadic.dta", replace


**# generate weighted averages *********************

use "C:\\...\\mirrortrade_dyadic.dta", clear

** merge with C.I.F. rate (dyad mean)
merge m:1 trade_pair using "C:\\...\\cif_dyadmeans.dta", keepusing(cifrate_mean)

** Drop trade pairs with no recorded trade history
drop if _merge != 3
drop _merge

*save "C:\\...\\mirrortrade_dyadic_wgtavgs.dta", replace

** create multiplicator for CIF values to generate approximate FOB values
gen fob_conversionfct = 1- cifrate_mean

** generare approx FOB value
gen imf_flow1_fob = fob_conversionfct* flowBAA

** calculate discrep_sharemirrorsum
gen abba_fob = imf_flow1_fob - flowBAB
gen abbabs_fob = flowBAB - imf_flow1_fob

** Only use observation for which both trade partners provide information
replace abba_fob=. if pr_flowBAA==1
replace abba_fob=. if pr_flowBAB==1

generate abba_mirrorsum=(abbabs_fob/(imf_flow1_fob+flowBAB))

** Check 
/*
sum abba_mirrorsum, detail
histogram abba_mirrorsum
browse if abba_mirrorsum>0.9 
histogram abba_mirrorsum if imf_flow1_fob>0 & flowBAB>0
*/

*save "C:\\...\\mirrortrade_dyadic_wgtavgs.dta", replace


**# generate weighted averages ***********************

*use "imfdotsmirror-uni-wgtavgs_stef.dta", clear

** generate median for A
bysort countryA_name (year): egen abba_medianA=median(abba_mirrorsum)

** same for B
bysort countryB_name (year): egen abba_medianB=median(abba_mirrorsum)

** calculate differences
generate diff_abbamedian=abs(abba_medianA-abba_medianB)

** Calculate means
generate cwgtA=.
replace cwgtA=(0.5+(diff_abbamedian/2)) if abba_medianA<=abba_medianB
replace cwgtA=(0.5-(diff_abbamedian/2)) if abba_medianA>abba_medianB

generate cwgtB=.
replace cwgtB=(0.5+(diff_abbamedian/2)) if abba_medianB<=abba_medianA
replace cwgtB=(0.5-(diff_abbamedian/2)) if abba_medianB>abba_medianA 	

generate cwgtsum=cwgtA+cwgtB

generate flowWAV=(cwgtA*imf_flow1_fob)+(cwgtB*flowBAB)

*browse countryA_name countryB_name year imf_flow1_fob flowBAB abba_medianA abba_medianB cwgtA cwgtB flowWAV

** Only use observation for which both trade partners provide information
replace flowWAV=. if pr_flowBAA==1
replace flowWAV=. if pr_flowBAB==1

** Keep necessary variables only
keep trade_pair year countryA_name countryB_name flowBAA pr_flowBAA flowBAB pr_flowBAB flowWAV

** save as dta
save "C:\\...\\mirrortrade_dyadic.dta", replace

** save as csv
export delimited "C:\\...\\mirrortrade_dyadic.csv", replace

